//
//  TwoViewController.m
//  KVOController的使用总结
//
//  Created by 唐斌 on 2018/6/28.
//  Copyright © 2018年 ruanton. All rights reserved.
//
//NSObject Category

//For an even easier usage, just #import <KVOController/NSObject+FBKVOController.h> for an automatic KVOController property on all objects.意思是不需要声明kvocontroller这个属性就可以使得每个控制器都有这个属性.从而可以方便的调用这个属性的kvo方法
#import "TwoViewController.h"
#import <NSObject+FBKVOController.h>

@interface TwoViewController ()

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //方法1,KVOController方法添加监听者
    //监听到值改变后就调用某个方法
  [ self.KVOController observe:self.TwoVCTextField keyPath:@"text" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
      //KVOController方法监听到的对象属性内容改变
          NSLog(@"twovc change内容是%@",change);

  }];
    
    //方法2,系统原生方法
//        [self systemKVOUse];
    
    // Do any additional setup after loading the view from its nib.
}


-(void)systemKVOUse{
    //系统方法添加监听者,正确的写法
    [self.TwoVCTextField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
    
    //这样添加监听者会报错,不能这样写
//        [self addObserver:self.TwoVCTextField forKeyPath:@"text" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
    
}

//系统方法监听到的对象属性内容改变
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    NSLog(@"systemKVO change内容是%@",change);
 
    
}

- (void)dealloc {
    //系统原生方法添加的监听者必须移除监听者
    //11.4以下的系统要移除监听者才不会崩溃
//    [self.TwoVCTextField removeObserver:self forKeyPath:@"text"];


    
}

- (IBAction)backBtnClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];

}


- (IBAction)removeTexttFBtnClicked:(id)sender {
            [self.TwoVCTextField removeFromSuperview];

    
}


-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
